package com.dinadi.yidi.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dinadi.yidi.R;
import com.dinadi.yidi.bean.CreativeHotspotRequestEntity;
import com.dinadi.yidi.ui.activity.ClassificationActivity;
import com.dinadi.yidi.ui.activity.CreativeHotspotDetailsActivity;
import com.dinadi.yidi.ui.activity.ExhibitionMatchActivity;
import com.dinadi.yidi.utils.FormatCurrentData;
import com.dinadi.yidi.utils.ImageUtils;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;

/**
 * author: JiaYu.He
 * Date: 2017/3/24
 * Time: 11:21
 */

public class WatchingAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    Context mContext;
    private List<CreativeHotspotRequestEntity> mDatas = new ArrayList<>();

    public void addAllData(List<CreativeHotspotRequestEntity> dataList) {
        this.mDatas.addAll(dataList);
        notifyDataSetChanged();
    }

    public void clearData() {
        this.mDatas.clear();
    }

    public WatchingAdapter(Context context) {
        mContext = context;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView tv_title, tv_time, tv_label, star_num, read_num;
        ImageView img_article;
        LinearLayout layout_item, view_show;


        public ViewHolder(View itemView) {
            super(itemView);
            tv_title = (TextView) itemView.findViewById(R.id.tv_title);
            img_article = (ImageView) itemView.findViewById(R.id.img_article);
            tv_time = (TextView) itemView.findViewById(R.id.tv_time);
            tv_label = (TextView) itemView.findViewById(R.id.tv_label);
            star_num = (TextView) itemView.findViewById(R.id.star_num);
            read_num = (TextView) itemView.findViewById(R.id.read_num);
            layout_item = (LinearLayout) itemView.findViewById(R.id.layout_item);
            view_show = (LinearLayout) itemView.findViewById(R.id.view_show);
        }


    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_watching, parent, false);
        return new ViewHolder(v);

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        WatchingAdapter.ViewHolder itemViewHolder = (WatchingAdapter.ViewHolder) holder;
        itemViewHolder.tv_title.setText(mDatas.get(position).getTitle());
        String creatTime = mDatas.get(position).getCreateTime();
        String showCreatTime = FormatCurrentData.getTimeRange(creatTime);
        itemViewHolder.tv_time.setText(showCreatTime);
        //"类型 0-未分类 1-资讯类 2-设计师类 9-活动",
        if (mDatas.get(position).getType().equals("0")) {

        } else if (mDatas.get(position).getType().equals("1")) {
            itemViewHolder.tv_label.setText("新鲜看点");

        } else if (mDatas.get(position).getType().equals("2")) {
            itemViewHolder.tv_label.setText("人物专访");

        } else if (mDatas.get(position).getType().equals("9")) {
            itemViewHolder.tv_label.setText("精彩活动");
            itemViewHolder.view_show.setVisibility(View.GONE);
        }

        itemViewHolder.star_num.setText(mDatas.get(position).getStarNum());
        itemViewHolder.read_num.setText(mDatas.get(position).getReadNum());
        String imgUrl = mDatas.get(position).getCoverImg();

        if (imgUrl != null) {
            if (!imgUrl.equals("")) {
                ImageUtils.loadImageView(mContext, imgUrl, itemViewHolder.img_article);
            } else {
                ImageUtils.loadImageView(mContext, R.drawable.symbols_bitmap_home_page, itemViewHolder.img_article);
            }
        } else {
            ImageUtils.loadImageView(mContext, R.drawable.symbols_bitmap_home_page, itemViewHolder.img_article);

        }
        itemViewHolder.layout_item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MobclickAgent.onEvent(mContext, "Article", "文章点击");
                if (mDatas.get(position).getType().equals("0")) {

                } else if (mDatas.get(position).getType().equals("1")) {
                     /*新鲜看点*/
                    Intent intent = new Intent();
                    intent.setClass(mContext, CreativeHotspotDetailsActivity.class);
                    intent.putExtra("hotId", mDatas.get(position).getHotId());
                    mContext.startActivity(intent);


                } else if (mDatas.get(position).getType().equals("2")) {
                    /*人物专访*/
                    Intent intent = new Intent();
                    intent.setClass(mContext, CreativeHotspotDetailsActivity.class);
                    intent.putExtra("hotId", mDatas.get(position).getHotId());
                    mContext.startActivity(intent);
                } else if (mDatas.get(position).getType().equals("9")) {
                    /*每周赛事*/
                    Intent intent = new Intent();
                    intent.setClass(mContext, ExhibitionMatchActivity.class);
                    intent.putExtra("showId", mDatas.get(position).getHotId());
                    mContext.startActivity(intent);
                }
            }
        });

    }


    @Override
    public int getItemCount() {
        return mDatas.size();
    }


}
